﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Work.Hardware;
using UeyesAIS.DataDefinition;
using UeyesAIS.Interface;

namespace UeyesAIS.Service.Device
{
    /// <summary>
    /// eBus相机
    /// </summary>
    public class CameraEbus : ICameraAdapter
    {
        private bool _state;
        private CameraInfomation _param;
        private eBUSCamera _camera;
        public bool CameraState
        {
            get { return _state; }
        }
        public CameraEbus(CameraInfomation cInfo)
        {
            _param = cInfo;
        }


        public async Task Init()
        {
            #region 每个相机单独初始化，传入参数为相机索引，功能保留
            try
            {
                await Task.Factory.StartNew(ConnectCamera);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            #endregion 每个相机单独初始化，传入参数为相机索引
        }

        private void ConnectCamera()
        {
            try
            {
                _camera = new System.Work.Hardware.eBUSCamera(_param.MacAddr);
                if (_camera.CameraStatus == System.Work.Hardware.Camera.enumStatus.NoDriver)
                {
                    throw new ArgumentException(string.Format(_param.MacAddr + "相机未安装驱动！"));
                }
                if (_camera.CameraStatus == System.Work.Hardware.Camera.enumStatus.NoCameraIPConfig)
                {
                    _camera.SetGevCamIPConfiguration(_param.LocalAdapterName, _param.CameraIp, _param.SubnetMask, _param.DefaultGateway);
                    _camera.SetGevNetworkAdapterInfo(_param.LocalAdapterName, _param.LocalIP, _param.SubnetMask, _param.DefaultGateway);
                    _camera.Close();
                    _camera = new System.Work.Hardware.eBUSCamera(_param.MacAddr);
                }
                if (_camera != null && _camera.CameraStatus == System.Work.Hardware.Camera.enumStatus.Running)
                {
                    _state = true;
                    _camera.SetExternTrigger(3);
                    _camera.SetLineTriggerSource(false);
                    _camera.StartContinuouShot();
                    _camera.FrameReady += _camera_FrameReady;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                //throw new Exception(ex.Message);
            }
        }
        private void _camera_FrameReady(Bitmap bitmap)
        {
            if (ImageReady != null)
                ImageReady(_param.CameraName, bitmap);
        }

        public bool SetExprosetime(double extime)
        {
            bool flag = false;
            if (_state && extime > 14 && extime < 7000000)
            {
                _camera.SetExposureTime(extime);
                flag = true;
            }
            return flag;
        }

        public bool OneShot()
        {
            bool flag = false;
            if (_state)
            {
                _camera.SoftTrigger();
                flag = true;
            }
            return flag;
        }

        public bool ContinueShot()
        {
            bool flag = false;
            if (_state)
            {
                _camera.SetExternTrigger(0);//1-3不要设置
                flag = true;
            }
            return flag;
        }

        public bool StopShot()
        {
            bool flag = false;
            if (_state)
            {
                _camera.SetExternTrigger(3);
                _camera.SetLineTriggerSource(false);
                _camera.StartContinuouShot();
                flag = true;
            }
            return flag;
        }

        public bool Close()
        {
            bool flag = false;
            if (_state)
            {
                _camera.Close();
                flag = true;
            }
            return flag;
        }

        public event EventHandler<Bitmap> ImageReady;

        public bool AquireBitmap()
        {
            return OneShot();
        }


        public double GetExprosetime()
        {
            double extime = 0;
            if (_state)
            {
                extime = _camera.GetExposureTime();
            }
            return extime;
        }

        public double GetErrorCount()
        {
            double errorcount = 0;
            if (_state)
            {
                errorcount = _camera.GetErrorCount();
            }
            return errorcount;
        }
        public bool AquireBitmap(string mapname)
        {
            return OneShot();
        }

        public bool AquireBitmap(int mapindex)
        {
            return OneShot();
        }

        public string CameraName
        {
            get { return _param.CameraName; }
        }
    }
}
